package com.winning.sx.microframework.common.shiro;

import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.base.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

//实现AuthorizingRealm接口用户用户认证
public class CustomShiroRealm extends AuthorizingRealm {

    //用于用户查询
    @Autowired
    private EasyService easyService;
    @Autowired
    private UserService userService;


    //角色权限和对应权限添加
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String name = (String) principalCollection.getPrimaryPrincipal();
        //查询用户名称
//        SecurityProperties.User user = loginService.findByName(name);
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        for (Role role:user.getRoles()) {
//            //添加角色
//            simpleAuthorizationInfo.addRole(role.getRoleName());
//            for (Permission permission:role.getPermissions()) {
//                //添加权限
//                simpleAuthorizationInfo.addStringPermission(permission.getPermission());
//            }
//        }
        return simpleAuthorizationInfo;
    }

    //用户认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (authenticationToken.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String name = authenticationToken.getPrincipal().toString();
        String passwd = authenticationToken.getCredentials().toString();
        UserToken usertoken = (UserToken) authenticationToken;

        if (usertoken.getLoginName().equals("openid")) {
            passwd="";
        } else if (usertoken.getLoginName().equals("telephone")) {
            Map param=new HashMap();
            param.put("telephone",name);
            try{
                Map staff= easyService.queryEntity("BASE_staff",param);
                if(staff==null){
                    throw new UnknownAccountException();
                }
                if(staff.get("validState").toString().equals(Constains.SYS_VALIDFLAG_TRUE)){
                    passwd=staff.get("validCode").toString();
                }else{
                    throw new IncorrectCredentialsException();
                }
            }catch(SQLException e){
                throw new UnknownAccountException(e.getMessage());
            }catch(IncorrectCredentialsException e){
                throw e;
            }catch(UnknownAccountException e){
                throw e;
            }
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, passwd, getName());
        return simpleAuthenticationInfo;
    }
}